package database

import (
	"database/sql"
	"fmt"
	_ "github.com/ClickHouse/clickhouse-go"
	"goTool/common"
)

/*
   ### sql连接实例 ###
*/
func SqlConn(cfg DbConfig) *sql.DB {
	ConnStr := ""

	switch cfg.Dialect {
	case "clickhouse":
		ConnStr = fmt.Sprintf("tcp://%s:%d?password=%s&debug=%t&database=%s&username=%s",
			cfg.Host, cfg.Port, cfg.Password, cfg.Debug, cfg.DbName, cfg.UserName)
	}

	conn, err := sql.Open(cfg.Dialect, ConnStr)
	common.IsError(err)

	err = conn.Ping()
	common.IsError(err)

	return conn
}
